/*
 * @lc app=leetcode.cn id=1002 lang=typescript
 *
 * [1002] 查找共用字符
 */

// @lc code=start

// 坑点： 这个统计重复字符的次数
function commonChars(words: string[]): string[] {
    const [head, ...other] = words.map((i) => {
        const counter = new Map<string, number>();
        i.split("").forEach((char) => {
            counter.set(char, (counter.get(char) || 0) + 1);
        });
        return counter;
    });
    let final: string[] = [];
    [...head.keys()].forEach((i) => {
        let times = Math.min(
            head.get(i)!,
            ...other.map((counter) => counter.get(i)! || 0)
        );
        while (times) {
            final.push(i);
            times--;
        }
    });

    return final;
}
// @lc code=end
